﻿/*
* 语音插件（在线录制、上传音频文件）
* v 1.0.0
* jQuery UI 1.8.7
*
* Copyright 2013, TsingDa
*/
function onRecordVoiceCloseHandler() {
    //在线录制，点击关闭
    var obj = $("#tsingda_voice_rec");
    if ($("#btnClose", obj).is(":button")) {
        $("#btnClose", obj).trigger("click");
    }
}
function onRecordVoiceUploadErrorHandler() {
    //在线录制，失败
    alert("在线录制保存失败");
}
function onRecordVoiceUploadCompleteHandler() {
    //在线录制，成功
//    var str = "参数：";
//    for (var i = 0, len = arguments.length; i < len; i++) {
//        str += "\r\n" + arguments[i];
//    }
    //    alert(str);
    if (arguments.length === 2 && arguments[1] !== 'error') {
        var obj = $("#tsingda_voice_rec");
        $(":hidden[name='hdDuration']", obj).val(arguments[0]);
        $(":hidden[name='hdURL']", obj).val(arguments[1]);
        $("#btnSave", obj).trigger("click");
    }
    else {
        alert("上传失败");
    }
}
function onUploadCompleteHandler() {
    //上传音频，完成
//    var str = "参数：";
//    for (var i = 0, len = arguments.length; i < len; i++) {
//        str += "\r\n" + arguments[i];
//    }
//    alert(str);
    if (arguments.length === 2 && arguments[1] !== 'error') {
        var obj = $("#tsingda_voice_sond");
        $(":hidden[name='HdName']", obj).val(arguments[0]);
        $(":hidden[name='hdUrl']", obj).val(arguments[1]);
        $("#btnSave", obj).trigger("click");
    }
    else {
        alert("上传失败");
    }
}
function onUploadCloseHandler() {
    //上传音频，关闭
    var obj = $("#tsingda_voice_sond");
    if ($("#btnClose", obj).is(":button")) {
        $("#btnClose", obj).trigger("click");
    }
}
function onUploadErrorHandler() {
    //上传音频，失败
    alert("上传失败");
}
var TsingDa = TsingDa || {};
TsingDa.VoicePlugin = {
    Config: {
        REC_UploadURL: encodeURIComponent('http://' + window.location.host + '/upload/voice?type=rec'),
        Sound_UploadURL: encodeURIComponent('http://' + window.location.host + '/upload/voice?type=sound')
    },
    REC: {
        c: 'btnREC', t: '在线录制', h: 1, e: function () {
            var _this = this;
            var uploadurl = TsingDa.VoicePlugin.Config.REC_UploadURL;
            var jREC = $("#tsingda_voice_rec");
            if ($("#tsingda_voice_rec").is("div") === false) {
                var htmlCode = '<div id="tsingda_voice_rec"><embed id="objflashrec" width="220" height="166" wmode="transparent" type="application/x-shockwave-flash" pluginspage=" http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" wmode="opaque" quality="high" src="http://resource.yi8edu.com/tsingda/voice/RecordVoice.swf?uploadUrl=' + uploadurl + '"></embed><div style="text-align:right;display:none;"><input type="hidden" name="hdDuration" value=""/><input type="hidden" name="hdURL" value=""/><input type="button" id="btnSave" value="确定" /><input type="button" id="btnClose" value="取消" /></div></div>';
                jREC = $(htmlCode);
                var jValue = $('#xheCodeValue', jREC),
                        jSave = $('#btnSave', jREC),
                        jClose = $('#btnClose', jREC);
                jSave.click(function () {
                    _this.loadBookmark();
                    var duration = $('#tsingda_voice_rec :hidden[name="hdDuration"]').val();
                    var url = $('#tsingda_voice_rec :hidden[name="hdURL"]').val();
                    var html = '<img class="voice_rec" src="http://192.168.2.101:808/images/recorded.gif" alt="" contentEditable="false" name="' + duration + '" url="' + url + '"/>&nbsp;';
                    _this.pasteHTML(html);
                    _this.hidePanel();
                    return false;
                });
                jClose.click(function () {
                    _this.hidePanel();
                });
            }
            _this.saveBookmark();
            _this.showDialog(jREC);
        }
    },
    SetREC: function (url, duration) {
        var obj = $("#tsingda_voice_rec");
        if ($(obj).is("div") && $(obj).find("#xheSave").is("input")) {
            $('#tsingda_voice_rec :hidden[name="hdDuration"]').val(duration);
            $('#tsingda_voice_rec :hidden[name="hdURL"]').val(url);
            $(obj).find("#xheSave").trigger("click");
        } else {
            alert("error");
        }
    },
    Sound: {
        c: 'btnSound', t: '上传音频', h: 1, e: function () {
            var _this = this;
            var uploadurl = TsingDa.VoicePlugin.Config.Sound_UploadURL;
            var jREC = $("#tsingda_voice_sond");
            if ($("#tsingda_voice_sond").is("div") === false) {
                var htmlCode = '<div id="tsingda_voice_sond"><embed id="objflashsound" width="170" height="137" wmode="transparent" type="application/x-shockwave-flash" pluginspage=" http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" wmode="opaque" quality="high" src="http://resource.yi8edu.com/tsingda/voice/Upload.swf?uploadUrl=' + uploadurl + '"></embed><div style="text-align:right;display:none;"><input type="hidden" name="HdName"  value=""/><input type="hidden" name="hdUrl"  value=""/><input type="button" id="btnSave" value="确定" /><input type="button" id="btnClose" value="取消" /></div></div>';
                jREC = $(htmlCode);

                var jValue = $('#xheCodeValue', jREC),
            jSave = $('#btnSave', jREC),
            jClose = $('#btnClose', jREC);
                jSave.click(function () {
                    _this.loadBookmark();
                    var name = $("#tsingda_voice_sond :hidden[name='HdName']").val(),
                    url = $("#tsingda_voice_sond :hidden[name='hdUrl']").val();
                    var html = '<img class="voice_song" alt="" src="http://192.168.2.101:808/images/wei_file.gif" contentEditable="false" name="' + name + '" url="' + url + '"/>&nbsp;';
                    _this.pasteHTML(html);
                    _this.hidePanel();
                    return false;
                });
                jClose.click(function () {
                    _this.hidePanel();
                });
            }
            _this.saveBookmark();
            _this.showDialog(jREC);
        }
    },
    SetSound: function (url, name) {
        var obj = $("#tsingda_voice_sond");
        if ($(obj).is("div") && $(obj).find("#btnSave").is("input")) {
            $(obj).find(":hidden[name='HdName']").val(name);
            $(obj).find(":hidden[name='hdUrl']").val(url);
            $(obj).find("#btnSave").trigger("click");
        } else {
            alert("error" + $(obj).find(":hidden").length);
        }
    }
};
